﻿
Dcqtech_Form_Controls.AttachList = $.extend({}, Dcqtech_Form_Controls.Default);

Dcqtech_Form_Controls.AttachList.setValue = function (id, value, fieldInfo, $root, isFirstSet) {

    // dcq_attachlist_init(id, value);
    var length = 0;
    if (value) {
        length = value.split(',').length;
    }
    $root.find("[dcqtech_data='" + id + "']").val(value);
    $root.find("[dcqtech_id='" + id + "'] input.form-control").val(length + "个附件。");
    $root.find("[dcqtech_id='" + id + "'] span.files-tips").val(length + "个附件");
    if(value){
        loadFiles(id, value)
    }
};

Dcqtech_Form_Controls.AttachList.setDefaultValue = function (id, $root, fieldInfo) {
    var value = fieldInfo.defaultValue;
    $root.find("[dcqtech_id='" + id + "'] input.form-control").val("0个附件。");
    let file_cell = $("[dcqtech_id=" + id +"]")
    file_cell.addClass("file-cell");
    file_cell.find(".form-control").hide()
    file_cell.parent().next(".control-label").addClass("file-cell-control")
    file_cell.find(".input-group-btn").hide()
    file_cell.append("<span class='files-tips'>0个附件</span><i class='iconfont oa-jiantou'></i>")

    let fileTarget = {id: id, filesList: [], showFilesManager: false}
    vm.filesTargets.push(fileTarget)
    if (!value) return;
    Dcqtech_Form_Controls.AttachList.setValue(id, value, fieldInfo, $root);
};

Dcqtech_Form_Controls.AttachList.setReadonly = function (id, isReadOnly) {
    if (typeof isReadOnly != "boolean") {
        isReadOnly = isReadOnly == "1";
    }
    if (isReadOnly) {
        $("[dcqtech_id='" + id + "'] .fileinput-button").hide();
    }
};

Dcqtech_Form_Controls.AttachList.setEditEvent = function (id, wdInfo, $root) {
    $("[dcqtech_id='" + id + "']").on("click",function () {
        console.log("------")
        vm.filesTargets.forEach((item)=>{
            if(item.id == id){
                item.showFilesManager = true
            }
        })
    })
};

Dcqtech_Form_Controls.AttachList.setViewEvent = function (id, fieldInfo, $root) {
    var $control = $root.find("[dcqtech_id='" + id + "']");
    $control.find(".fileinput-button").hide();
}


function ToHumanSize(size) {
    if (size > 1024 * 1024 * 1024) {
        return (size / (1024 * 1024 * 1024)).toFixed(2) + "G";
    }
    else if (size > 1024 * 1024) {
        return (size / (1024 * 1024)).toFixed(2) + "M";
    }
    else if (size > 1024) {
        return (size / (1024)).toFixed(2) + "K";
    }
    else {
        return size + "B";
    }
}

// var DcqtechWordFileInfos = wm || {};

function showDcqtechAttachListHtme(id) {
//     var infos = DcqtechWordFileInfos[id];
//     var isReadonly = Dcqtech_Form.formInfos.fieldInfos[id].isReadOnly == "1";
//     if(isReadonly) $("[dcqtech_id='" +id + "'] .fileinput-button").hide();
//     if (!infos || (infos.length === 0)) {
//         $("[dcqtech_id='" + id + "'] ul.list-group").html("");
//         $("[dcqtech_data='" + id + "']").val("");
//     } else {
//         var html = [];
//         var fileids = [];
//         for (var i = 0; i < infos.length; i++) {
//             var fileInfo = infos[i];
//             html.push('<li class="clearfix  list-group-item" index="', i, '" id="', fileInfo.GUID, '"><a target="_blank" href="', Dcqtech.Url.BaseUrl, 'WorkFlowRuntime/Attachment/download?id=', fileInfo.GUID, '"><span role="filename">', fileInfo.FILENAME, '</span></a>');
//             html.push('  <span class="label label-primary" > ', fileInfo.SIZE, '</span>');
//             html.push('<span class="pull-right" >');
// //            html.push(' <a class="btn btn-info btn-xs" role="download" href="', Dcqtech.Url.BaseUrl, 'WorkFlowRuntime/Attachment/download?id=', fileInfo.GUID, '" > <i class="fa fa-download" > </i> 下载</a> ');
//             if (Dcqtech_Form.isEdit && !isReadonly) html.push(' <button class="btn btn-danger btn-xs" role="delete" onclick="dcq_attachlist_delete(this);" > <i class="fa fa-remove" > </i> 删除</button> ');
//             html.push('</span>');
//             html.push('</li>');
//             fileids.push(fileInfo.GUID);
//         }
//         $("[dcqtech_id='" + id + "'] ul.list-group").html(html.join(''));
//         $("[dcqtech_data='" + id + "']").val(fileids.join(','));
//     }
}

var dcqtech_current_edit_wordinfo = {};



var websocket_AttachList;

function dcq_attachlist_delete(obj) {
    var $this = $(obj);
    var index = parseInt($this.parents("li.list-group-item").attr("index"), 10);
    var id = $this.parents("[dcqtech_id]").attr("dcqtech_id");
    var fileInfos = DcqtechWordFileInfos[id];
    var wordId = fileInfos[index].GUID;
    var filename = fileInfos[index].FILENAME;
    if (confirm("确定要删除文档：“" + filename + "”吗?")) {
        DcqtechWordFileInfos[id].splice(index, 1);
        showDcqtechAttachListHtme(id);
    }
}




function dcq_attachlist_init(id, ids) {

    if (ids) {
        Dcqtech.getJSON("~/WorkFlowRuntime/Attachment/GetByIds?ids=" + ids, function (ret) {
            if (ret && ret.length) {
                for (var i = 0; i < ret.length; i++) {
                    ret[i].SIZE = ToHumanSize(ret[i].SIZE);
                }
            }
            DcqtechWordFileInfos[id] = ret;
            showDcqtechAttachListHtme(id);
            console.info(ret);
        });
    } else {
        showDcqtechAttachListHtme(id);
    }



}
